/**该数据转换为大数据的存储要求的数据格式 */
import { dataPrice24To96, extractBetweenStrings } from "@src/utils/common";
import { chineseToKey } from "./enum";
import { stringToNum } from './tool'
import { ConfigType } from "./type"; 
import { getAccountNodeInfo } from "@src/config/accountNode";
/**
 *  处理 市场披露信息（预测-出清前）、市场披露信息（实际-临时）数据
 * @param originData 原始数据
 * @param date 数据日期
 * @param type 数据源类型
 */
export const marketDulDisclosureBackEndDul = (originData:any,date:string,config:ConfigType)=>{
    /** 获取每个时刻对应的键值对 */
    const getObjData = (keyArr:string[], dataArr:string[][],dataItemIndex:number)=> {
       const resultObj:any = {} 
       keyArr.forEach((item:string,index:number)=>{
        resultObj[chineseToKey[item]] = stringToNum(dataArr[index][dataItemIndex])
       })
       return resultObj
    }
    const resultData = {
    [config.type]:originData?.pointList?.map((item:any,index:number)=>{
      return {
        runDate:date + ' ' +config.timeArr[index],
        oriRunDate:date + ' ' +config.originTimeArr[index],
        ...getObjData(originData.listName,originData.Val,index)
      }
     }),
     date
   }
   return resultData
}


/**
 *  处理 抽蓄数据（预测-出清前）数据
 * @param originData 原始数据
 * @param date 数据日期
 * @param type 数据源类型
 */
export const storageDataBackEndDul = (originData:any,date:string,config:ConfigType) => {

  const resultData  = {
    [config.type]:originData?.chartAttr?.series[0]?.data?.map((item:any,index:number)=>{
      return {
        runDate:date + ' ' +config.timeArr[index],
        oriRunDate:date + ' ' +config.originTimeArr[index],
        [chineseToKey['抽蓄数据']]:stringToNum(item.y)
      }
    }),
    date
  }
  return resultData
}

/**
 * 处理 用户侧结算价-日前、用户侧结算价（实时-临时）、用户侧结算价（实时-正式） 
 * 若是24点数据 需 转为96点
 * @param originData 原始数据
 * @param date 数据日期
 * @param type 其它配置
 */
export const twentyDourSwitchDot = (dataSource:any,date:string, config:ConfigType)=>{
  let originVal = dataSource?.jsVal || dataSource?.list_zhcqj
  if(originVal && originVal?.length === 24) originVal = dataPrice24To96(originVal)

  const resultData = {
    [config.type]:originVal?.map((item:string,index:number)=>{
      return {
         runDate: date + ' ' + config.timeArr[index],
         oriRunDate:date + ' ' +config.originTimeArr[index],
         [chineseToKey[config.nameToKey!]]:stringToNum(item)
       } 
     }),
     date
  }
  return resultData
}

/**处理 备用信息（预测-出清前）、备用信息（实际-临时）、备用信息（实际-正式） */
export const beiYongDul = (dataSource:any,date:string, config:ConfigType)=>{
  const result:any = []
  config.timeArr.forEach((item:string,index:number)=>{
    result.push({
      runDate:date + ' ' + config.timeArr[index],
      oriRunDate:date + ' ' +config.originTimeArr[index],
    })
    dataSource?.data?.forEach((itemD:any)=>{
        if(item === itemD.PERIODID){
          if(itemD.TYPE === '正备用'){
            result[index-1][chineseToKey['正备用']] = itemD[config.nameToKey!]
          }
          if(itemD.TYPE === '负备用'){
            result[index-1][chineseToKey['负备用']] = itemD[config.nameToKey!]
          }
        }
        /**针对 24:00 做特殊处理，归集与 23:45 */
        if(itemD.PERIODID === "24:00"){
          if(itemD.TYPE === '正备用'){
            result[index][chineseToKey['正备用']] = itemD[config.nameToKey!]
          }
          if(itemD.TYPE === '负备用'){
            result[index][chineseToKey['负备用']] = itemD[config.nameToKey!]
          }
        }
    })
  })
  const resultData = {
    [config.type]:result.length ? result : undefined,
    date
  }
  return resultData
}

// 必开必停机组信息（预测-出清前）
export const aheadOffonInfoDul = (dataSource:any,date:string, config:ConfigType)=>{
  const { data } = dataSource
   const result =  {
      runDate: data[0]?.DATE,
      [chineseToKey['必开']]:data[0]?.OPENCAPACITY,
      [chineseToKey['必停']]:data[0]?.CLOSECAPACITY
    }
    const resultData = {
       [config.type]:result,
       date
    }
   return resultData
}

// 事前监管（预测-出清前）
export const  realtimeRegulatedInfoDul = (dataSource:any,date:string, config:ConfigType)=>{
  const { data } = dataSource
  const content = data[0]?.content
  let val = null
  if(/^触发事前监管/.test(content)) val = 1
  if(/^未触发事前监管/.test(content)) val = 0
  const result = {
      runDate: data[0]?.DATE,
      [chineseToKey['事前监管']]: val,
      [chineseToKey['平均供需比']]: extractBetweenStrings(content, '平均供需比为', ';'),
      [chineseToKey['高峰供需比']]: extractBetweenStrings(content, '高峰供需比为', ';')
    }

  const resultData = {
    [config.type]:result,
    date
  }
  return resultData
}

// 节点电价（实时-临时）
export const realtimeNodePriceInfoDul = (dataSource:any,date:string, config:ConfigType)=>{
  const {nodeName = '', nodeId = ''} = getAccountNodeInfo()
  
    const result = dataSource?.data?.map((item:any,index:number)=>{
       return {
         runDate:date + ' ' + config.timeArr[index],
         oriRunDate:date + ' ' +config.originTimeArr[index],
         [chineseToKey['节点电价']]: item.cqPrice,
         [chineseToKey['节点名称']]: nodeName,
         [chineseToKey['节点id']]: nodeId
       }
    })
    const resultData = {
      [config.type]:result,
      date
    }
    return resultData
}

// 全省节点电价（实时、日前）
export const nodePriceAvg = (dataSource:any,date:string, config:ConfigType)=>{  
    const result = dataSource?.data?.map((item:any,index:number)=>{
       return {
         runDate:date + ' ' + config.timeArr[index],
         [chineseToKey['节点电价']]: item.Nodeprice
       }
    })
    const resultData = {
      [config.type]:result,
      date
    }
    return resultData
}